/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

class EPlane 
	{
	public:
		EVec4	plane;			//	Ax + By + Cz + Dw = 0
	
					EPlane			( void );
					EPlane			( float a, float b, float c, float d );
					EPlane			( const float *p );

		EVec4		GetVector		( void ) const;					
		EVec4		GetNormal		( void ) const;
		EVec4		DistVec			( const EVec4 &point ) const;
		float		Dist			( const EVec4 &point ) const;
		int			Classify		( const EVec4 &point ) const;
		
		EPlane		&Make			( const EVec4 &p0, 	const EVec4 &p1, const EVec4 &p2 ); 
		EPlane		&Make			( const EVec4 &norm, const EVec4 &point );
		
		void		NormalizeSelf	( void );
		EPlane		Normalize		( void ) const;
		
		void		Transform		( const EMatrix4 &T );
		
		EVec4		Project			( const EVec4 &orig ) const;
		float		Intersection	( const EVec4 &p0, const EVec4 &v ) const;
		
		bool		operator==		( const EPlane &p ) const { return ( this->plane==p.plane ); }
		bool		operator!=		( const EPlane &p ) const { return ( this->plane!=p.plane ); }
	};
	



inline EPlane::EPlane(void) {
	plane = EVec4(0,0,1,0);
}

inline EPlane::EPlane( float a, float b, float c, float d ) {
	plane = EVec4(a,b,c,d);
}

inline EPlane::EPlane( const float *p ) {
	plane = EVec4(p);
}



inline EVec4 EPlane::GetVector(void) const {
	return plane;
}

inline EVec4 EPlane::GetNormal(void) const {
	EVec4 temp = plane;
	temp.w = 0;
	return temp;
}


inline int EPlane::Classify(const EVec4 &point) const {
	float dot = point | plane;
	if (dot>0.0f) return 1;
	if (dot<0.0f) return -1;
	return 0;
}


inline EVec4 EPlane::DistVec(const EVec4 &point) const {
	return EVec4();
}


inline void EPlane::Transform(const EMatrix4 &T)
{
	EMatrix3	temp = T.Minor(3,3);
	EVec3	s( T.M[0][3], T.M[1][3], T.M[2][3] );
	EVec3	n( plane.x, plane.y, plane.z );
	n = temp*n;
	float d = plane.w - (n|s);
	plane = EVec4(n.x, n.y, n.z, d);
}



inline EPlane &EPlane::Make(const EVec4 &norm, const EVec4 &point)
{
	EVec4	n = norm.Normalize();
	plane	=	n;
	plane.w	=	- (n | point);
	return *this;
}


inline void EPlane::NormalizeSelf(void)
{
	float len = GetNormal().Length();
	ASSERT(len);
	
	plane *= (1.0f/len);
}

inline EPlane EPlane::Normalize(void) const
{
	EPlane temp = *this;
	temp.NormalizeSelf();
	return temp;
}


inline EVec4 EPlane::Project(const EVec4 &orig) const
{
	EVec4	normal = GetNormal();
	
	float t = - ( plane | orig )/( plane | normal );
	return normal*t + orig;
}


inline float EPlane::Intersection(const EVec4 &p0, const EVec4 &v) const
{
	ASSERT(plane | v);
	return - ( ( plane | p0 ) / ( plane | v) );
}



